/*
 * Copyright (C) 2011 Macambira Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.googlecode.control;

import com.googlecode.entity.Curso;
import com.googlecode.exception.CursoNaoEncontradoException;
import com.googlecode.persistence.CursoDAO;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Classe responsavel por realizar o CRUD de curso
 * 
 * @version 0.1 29 Set 2011
 * @author Tony Silva | tony.silva@dce.ufpb.br
 */
public class ControlCurso {
    
    private List<Curso> cursos;
    private CursoDAO dao;

    public ControlCurso() {
        this.cursos = new LinkedList<Curso>();
        this.dao = new CursoDAO();
    }

    public void addCurso(Curso curso) throws SQLException {
        try {
         
                //        this.cursos.add(curso);
            dao.CadastrarCurso(curso);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    public Curso getCursoNome(String nome)throws SQLException {
        try {
                //        for(Curso d: cursos) {
                //            if (d.getNome().equals(nome)) {
                //                return d;
                //            }
                //        }
                //throw new CursoNaoEncontradoException("Curso nao encontrado");
                
            return dao.ConsultaCursoNome(nome);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return null;
    }

    public Curso getCursoId(String id) throws SQLException {
        try {
                //        for(Curso d: cursos) {
                //            if (d.getId().equals(id)) {
                //                return d;
                //            }
                //        }
                //throw new CursoNaoEncontradoException("Curso nao encontrado");
                
            return dao.ConsultaCursoId(id);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        return null;
    }

    public void removeCursoNome(String nome) throws SQLException {
        try {
                //        for(Curso d: cursos) {
                //            if (d.getNome().equals(nome)) {
                //                this.cursos.remove(d);
                //                break;
                //            }
                //        }
            dao.RemoverCursoNome(nome);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
    }

    public void removeCursoId(String id) throws SQLException {
        try {
                //        for(Curso d: cursos) {
                //            if (d.getId().equals(id)) {
                //                this.cursos.remove(d);
                //                break;
                //            }
                //        }
            dao.RemoverCursoId(id);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        
    }

    public int getSize() {
        return this.cursos.size();
    }
    
    public List getListaCursos(){
        return this.cursos;
    }
    
}